package vn.nichecafe.client.ui.slider.widget;

import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.Effects;

import java.util.List;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;

public class SliderPanel extends Composite {

	private int duration;


	private List<ListItems> listItems;

	private int currentFrame;

	private Button btnNext;

	private Button btnPreview;

	private UnorderedList unorderedList;

	private int width;

	public SliderPanel() {
	}

	public SliderPanel(List<ListItems> listItems, int width, int height,
			int duration) {
		this.duration = duration;
		this.listItems = listItems;
		this.width = width;

		// Init containner
		FlowPanel flowPanel = new FlowPanel();
		flowPanel.setStyleName("slider");
		flowPanel.setPixelSize(width, height);
		this.initWidget(flowPanel);

		// init next button
		btnNext = new Button("Next");
		btnNext.setStyleName("nextBtn");
		btnNext.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				next();
			}
		});

		// init preview button
		btnPreview = new Button("Preview");
		btnPreview.setStyleName("prevBtn");
		btnPreview.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				preview();
			}
		});

		// add button to containner
		flowPanel.add(btnNext);
		flowPanel.add(btnPreview);

		// add ul widget
		unorderedList = new UnorderedList();
		unorderedList.setWidth(String.valueOf(width * listItems.size()) + "px");
		flowPanel.add(unorderedList);

		// add content
		for (ListItems lItems : listItems) {
			lItems.setPixelSize(width, height);
			unorderedList.add(lItems);
		}

		checkFirstLast();
	}

	public void next() {
		currentFrame++;
		slide();
	}

	public void preview() {
		currentFrame--;
		slide();
	}

	public void start() {

	}

	public void stop() {

	}

	private void slide() {
		btnNext.setEnabled(false);
		btnPreview.setEnabled(false);
		String left = String.valueOf(width * currentFrame * -1) + "px";
		$(unorderedList.getElement()).as(Effects).animate(
				"left:'" + left + "'", duration, Easing.LINEAR, new Function() {
					public void cancel(Element e) {
						
					}
					public void f(Element e) {
						btnNext.setEnabled(true);
						btnPreview.setEnabled(true);
					}
				});
		checkFirstLast();
	}

	private void checkFirstLast() {
		btnPreview.setVisible(currentFrame != 0);
		btnNext.setVisible(currentFrame != listItems.size() - 1);
	}

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}


}
